﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using omsData;
namespace myomscl_web.Admin.UserControls
{
    public partial class ucUserType : System.Web.UI.UserControl
    {
        public string UserTypeId
        {
            get
            {
                if (Session["userTypeId"]!=null)
                return Session["userTypeId"].ToString();
                return null;
            }
            set
            {
                Session["userTypeId"] = value;
            }
        }
       
        public void LoadUserType()
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                if (!string.IsNullOrEmpty(UserTypeId))
                {
                    int uti = Int32.Parse(UserTypeId);
                    var userType = context.UserTypes.Where(ut => ut.Id == uti).First();
                    txtUserTypeName.Text = userType.Name;
                    LoadLists(context,userType);
                }
                else
                {
                    txtUserTypeName.Text = string.Empty;
                    ucsSelectedList.Visible = false;
                }
            }

            this.Visible = true;
        }

        private void LoadLists(OmsModelContainer context, UserType userType)
        {
            
                var allListScreens = context.ListScreens.ToList();
                var selectedListScreens = userType.ListScreens.ToList();
                var unselectedListScreens = allListScreens.Except(selectedListScreens).ToList();                

                ucsSelectedList.LoadSources(unselectedListScreens.ConvertAll(new Converter<ListScreens, ListItem>(ListScreenToListItem)),
                    selectedListScreens.ConvertAll(new Converter<ListScreens, ListItem>(ListScreenToListItem)));
            
        }

        private ListItem ListScreenToListItem(ListScreens lst)
        {
            return new ListItem(lst.Name, lst.Id.ToString());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                if (string.IsNullOrEmpty(UserTypeId))
                {
                    if (!context.UserTypes.Where(ut => ut.Name == txtUserTypeName.Text).Any())
                    {
                        var newUserType = new UserType()
                        {
                            Name = txtUserTypeName.Text
                        };

                        context.AddToUserTypes(newUserType);
                    }
                }
                else
                {
                    int uti = Int32.Parse(UserTypeId);
                    if (!context.UserTypes.Where(ut => ut.Name == txtUserTypeName.Text&&ut.Id!=uti).Any())
                    {
                        var userType = context.UserTypes.Where(ut => ut.Id== uti).First();
                        userType.Name = txtUserTypeName.Text;


                        var listScreens = this.ucsSelectedList.GetSelected();
                        userType.ListScreens.Clear();
                        foreach (ListItem lst in listScreens)
                        {
                            int id = Int32.Parse(lst.Value);
                            var c = context.ListScreens.Where(cc => cc.Id == id).First();
                            userType.ListScreens.Add(c);
                        }
                    }
                }

                    context.SaveChanges();
                    ModifyingUserType(txtUserTypeName.Text);
            }
        }

        public delegate void OnModifyingUserType(string newTypeName);

        public event OnModifyingUserType ModifyingUserType;
    }
}